﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Mind.Data
{
    public class WrapperMessage
    {
        public WrapperMessageType Type { get; private set; }
        public string Message { get; private set; }
        public Exception Exception { get; private set; }

        public WrapperMessage(WrapperMessageType type, string message, Exception exception)
        {
            this.Type = type;
            this.Message = message;
            this.Exception = exception;
        }
    }

    public enum WrapperMessageType
    {
        Information,
        Warning,
        Error,
        FatalError
    }

    public class WrapperInvalidException : ApplicationException
    {
        public WrapperMessage[] Messages { get; private set; }
        public WrapperInvalidException(WrapperMessage[] messages): base(FormatMessages(messages))
        {
            this.Messages = messages;
        }

        private static string FormatMessages(WrapperMessage[] messages)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("This instance is invalid:");
            foreach (WrapperMessage item in messages)
            {
                sb.AppendFormat("- {0}: {1}", item.Type, item.Message);
                sb.AppendLine();
            }

            if (messages == null || messages.Length == 0)
                sb.AppendLine("No reason informed.");

            return sb.ToString();
        }
    }
}
